{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from bokeh.models import ColumnDataSource, CustomJS, Rect\n",
    "from bokeh.plotting import output_notebook, figure, show\n",
    "from bokeh.layouts import row\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 20\n",
    "img = np.empty((N, N), dtype=np.uint32)\n",
    "view = img.view(dtype=np.uint8).reshape((N, N, 4))\n",
    "for i in range(N):\n",
    "    for j in range(N):\n",
    "        view[i, j, 0] = int(i/N*255)\n",
    "        view[i, j, 1] = 158\n",
    "        view[i, j, 2] = int(j/N*255)\n",
    "        view[i, j, 3] = 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ColumnDataSource({'x':[], 'y':[], 'width':[], 'height':[]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "JSCODE = \"\"\"\n",
    "var data = source.data;\n",
    "var start = cb_obj.start;\n",
    "var end = cb_obj.end;\n",
    "data[%r] = [start + (end - start) / 2];\n",
    "data[%r] = [end - start];\n",
    "\n",
    "// this is needed because we modified .data \"in place\"\n",
    "source.change.emit();\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = figure(title='Box Zoom Here', plot_width=400, plot_height=400,\n",
    "            x_range=(0,10), y_range=(0,10), tools ='box_zoom,wheel_zoom,pan,reset')\n",
    "p1.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])\n",
    "p1.x_range.callback = CustomJS(args=dict(source=source), code=JSCODE % ('x', 'width'))\n",
    "p1.y_range.callback = CustomJS(args=dict(source=source), code=JSCODE % ('y', 'height'))\n",
    "\n",
    "p2 = figure(title='See Zoom Window Here', plot_width=400, plot_height=400, \n",
    "            x_range=(0,10), y_range=(0,10), tools=\"\")\n",
    "p2.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])\n",
    "p2.rect('x', 'y', 'width', 'height', fill_alpha=0, line_color='black', source=source)\n",
    "\n",
    "show(row(p1, p2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
